//
//  MSBaseViewController.m
//  MyShare
//
//  Created by koki on 15/9/14.
//  Copyright (c) 2015年 MyShare. All rights reserved.
//

#import "MSBaseViewController.h"
#import "PingTransition.h"
#import "PingInvertTransition.h"

@interface MSBaseViewController ()

@end

@implementation MSBaseViewController
@synthesize contentView;

-(id)init
{
    self=[super init];
    if (self)
    {
        self.VctrID=[NSNumber numberWithInt:arc4random()];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.frame=[[UIScreen mainScreen]bounds];
    self.view.backgroundColor=[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateViewController:) name:MSViewControllerUpdateNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshViewController:) name:MSViewControllerRefreshNotification object:nil];
    [self initNavigationBar];
    [self initUI];
    [self testDataFunc];
}


- (void)dealloc
{
    for(UIView* view in [self.contentView subviews])
    {
        [view removeFromSuperview];
    }
    
    self.contentView=nil;
    self.VctrID=nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Custom event methods

-(void) initNavigationBar
{
    _navBarView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 64)];
    _navBarView.backgroundColor = UIColorFromRGB(0x272d33);
    [self.view addSubview:_navBarView];
    
    self.contentView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT-44)];
    self.contentView.clipsToBounds=YES;
    self.contentView.scrollEnabled=NO;
    self.contentView.scrollsToTop = NO;
    self.contentView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:self.contentView];
    
    //添加左导航按钮    
    leftBarBtn=[GXButton buttonWithType:UIButtonTypeCustom];
    [leftBarBtn setFrame:CGRectMake(5, 22, 60, 40)];
    leftBarBtn.imageViewFrame = CGRectMake(5, 10, 12, 20);
    leftBarBtn.relayout = YES;
    leftBarBtn.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;
    [leftBarBtn setImage:[UIImage imageNamed:@"backNavBarBtn"] forState:UIControlStateNormal];
    [leftBarBtn addTarget:self action:@selector(leftBarBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_navBarView addSubview: leftBarBtn];
    leftBarBtn.hidden=YES;
    
    //添加右导航按钮
    rightBarBtn=[GXButton buttonWithType:UIButtonTypeCustom];
    [rightBarBtn setFrame:CGRectMake(SCREENWIDTH-50, 20, 50, 44)];
    rightBarBtn.titleLabel.textAlignment=NSTextAlignmentRight;
    rightBarBtn.titleLabel.font = HeitiFont(17);
    [rightBarBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBarBtn.autoresizingMask=UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
    [rightBarBtn addTarget:self action:@selector(rightBarBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_navBarView addSubview: rightBarBtn];
    rightBarBtn.hidden=YES;
    
    //添加标题
    titleLable=[[UILabel alloc]initWithFrame:CGRectMake(50, 20, SCREENWIDTH-100, 44)];
    titleLable.backgroundColor=[UIColor clearColor];
    titleLable.textColor=[UIColor whiteColor];
    [titleLable setTextAlignment:NSTextAlignmentCenter];
    [titleLable setFont:[UIFont fontWithName:@"STHeitiSC-Light" size:18]];;
    [_navBarView addSubview:titleLable];
    
    lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, _navBarView.bottom-0.5, _navBarView.width, 0.5)];
    lineView.image = [UIImage imageNamed:@"dot"];
    [_navBarView addSubview:lineView];
}

-(void)updateViewController:(NSNotification *)note{}

-(void)refreshViewController:(NSNotification *)note{}

-(void)collectionStateChanged:(NSNotification *)note{}

-(void)initUI{}

-(void) testDataFunc{}

-(void)leftBarBtnAction
{
    [rootNavigation popViewControllerAnimated:YES];
}

-(void) rightBarBtnAction{}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)setEnableTransitionAnimation:(BOOL)enableTransitionAnimation
{
    _enableTransitionAnimation=enableTransitionAnimation;
    if (enableTransitionAnimation)
    {
        rootNavigation.delegate = self;
    }
    else
    {
        rootNavigation.delegate = nil;
    }
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView setAnimationsEnabled:YES];
    if (self.enableTransitionAnimation)
    {
        rootNavigation.delegate = self;
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.enableTransitionAnimation=NO;
}


- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC{
    
    if(!self.enableTransitionAnimation)
        return nil;
    
    if (operation == UINavigationControllerOperationPush)
    {
        
        PingTransition *ping = [PingTransition new];
        ping.startRect=self.transitionPushRect;
        return ping;
    }
    if (operation == UINavigationControllerOperationPop)
    {
        if (self.invertAnimation)
        {
            PingTransition *ping = [PingTransition new];
            ping.startRect=self.transitionPopRect;
            return ping;
        }
        
        PingInvertTransition *pingInvert = [PingInvertTransition new];
        pingInvert.endRect=self.transitionPopRect;
        return pingInvert;
    }
    else
    {
        return nil;
    }
}

@end
